#! /bin/bash

if [ -z "$(which conan)" ]; then
    echo "build_conan.sh: conan not found, please install it"
    exit 1
fi

BUILD_TYPE=Debug
BUILD_FOLDER=build_$(echo "${BUILD_TYPE}" | tr '[:upper:]' '[:lower:]')
CONAN_CMAKE_CACHE_FOLDER=.cache/conan_$(echo "${BUILD_TYPE}" | tr '[:upper:]' '[:lower:]')

if [ -d "${CONAN_CMAKE_CACHE_FOLDER}" ]; then
    echo "build_conan.sh: ${CONAN_CMAKE_CACHE_FOLDER} exists, the command 'conan install' will not run"
else
    conan install \
    --build=missing \
    -s build_type=${BUILD_TYPE} \
    --generator=CMakeDeps \
    --generator=CMakeToolchain \
    --requires=gtest/1.14.0 \
    --output-folder=${CONAN_CMAKE_CACHE_FOLDER}
fi

cmake -B ${BUILD_FOLDER} \
    -DCMAKE_TOOLCHAIN_FILE=${CONAN_CMAKE_CACHE_FOLDER}/conan_toolchain.cmake \
    -DCMAKE_BUILD_TYPE=${BUILD_TYPE} \
    -DTHIRD_PARTY_MODE="conan2"

cmake --build ${BUILD_FOLDER}

ctest --test-dir ${BUILD_FOLDER}
